package com.topscomm.gpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.gpm.GpmScript;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.pojo.GpmProjectRiskEntity;
import com.topscomm.gpm.service.impl.GpmProjectRiskService;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description TODO
 * @Classname SdProjectRiskScript
 * @Date 2021/12/18
 * @Created by jiangtao
 */
public class GpmProjectRiskScript extends GpmScript {
	//状态enum 用流程节点名字来同步风险开放状态
	private enum NodeName {
		Start {
			public String toString() {
				return "开始";
			}
		},
		Finish {
			public String toString() {
				return "结束";
			}
		}
	}

	private enum riskStatus {
		Open {
			public String toString() {
				return "0";
			}
		},
		Close {
			public String toString() {
				return "2";
			}
		}
	}

	/***
	 * @description: 同步风险状态和审核状态
	 * @param service
	 * @param entity
	 * @return
	 * @author: jiangtao1
	 * @date: 2021/12/18 9:43
	 * @modify:
	 */
	@Transactional
	public void SyncRiskStatus(ITapService service, TapEntity entity) throws Exception {
		//这一段放在后台是因为这是放到开始和结束节点的脚本，这两个节点没有对应的审批页面，也就没有对应的前端调用方法
		CboApprovalNodeInsEntity nodeinsentity = (CboApprovalNodeInsEntity) entity;
		GpmProjectRiskService riskService = (GpmProjectRiskService) SpringUtil.getBean("gpmProjectRiskService");
		GpmProjectRiskEntity Riskentity = riskService.queryById(ConvertUtil.convertToString(nodeinsentity.getSourceid()));
		//Risk status 与nodeins字段extendfield2(在流程中默认为节点开始名字，开始节点默认为开始，结束节点默认为结束)绑定
		//若当前风险状态为审核中,且尚未走到结束节点
		if (Riskentity.getState() == 1 && !nodeinsentity.getExtendfield2().contentEquals(NodeName.Finish.toString())) {
			Riskentity.setRiskstatus(riskStatus.Open.toString());
		} else {
			Riskentity.setRiskstatus(riskStatus.Close.toString());
		}
		riskService.updateEntity(Riskentity);
	}

	/***
	 * @description: 同步项目最新项目经理信息
	 * @param service
	 * @param entity
	 * @return
	 * @author: jiangtao1
	 * @date: 2021/12/18 9:44
	 * @modify:
	 */
	@Transactional
	public void syncProjectInfo(ITapService service, TapEntity entity) throws Exception {
		//获取项目entity和关联的Projec的Entity
		CboApprovalNodeInsEntity nodeInsEntity = (CboApprovalNodeInsEntity) entity;
		GpmProjectRiskService riskService = (GpmProjectRiskService) SpringUtil.getBean("gpmProjectRiskService");
		GpmProjectRiskEntity RiskEntity = riskService.queryById(ConvertUtil.convertToString(nodeInsEntity.getSourceid()));
		GpmProjectService projectService = (GpmProjectService) SpringUtil.getBean("gpmProjectService");
		GpmProjectEntity projectEntity = projectService.queryById(ConvertUtil.convertToString(RiskEntity.getProjectid()));
		//获取当前项目经理和工号,不匹配则更新
		boolean managerChanged = !projectEntity.getProjectmanagercode().contentEquals(RiskEntity.getProjectmanagercode());
		boolean levelChanged = !projectEntity.getProjectlevel().contentEquals(RiskEntity.getProjectlevel());
		//项目经理信息变更
		if (managerChanged) {
			RiskEntity.setProjectmanager(projectEntity.getProjectmanager());
			RiskEntity.setProjectmanagercode(projectEntity.getProjectmanagercode());
		}
		//项目等级变更
		if (levelChanged) {
			RiskEntity.setRisklevel(projectEntity.getProjectlevel());
		}
		//任意一个变更则更新
		if (managerChanged || levelChanged) {
			riskService.updateEntity(RiskEntity);

		}

	}
}
